public class BinaryTree2{

    class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int val){
            this.val = val;
        }
    }
    //前序遍历
    public void preOrder(TreeNode root){
        if(root == null){
            return ;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    public TreeNode createBinaryTree1() {
        TreeNode root1 = new TreeNode(1);
        TreeNode root2 = new TreeNode(2);
        TreeNode root3 = new TreeNode(3);
        TreeNode root4 = new TreeNode(4);
        TreeNode root5 = new TreeNode(5);
        TreeNode root6 = new TreeNode(6);
        TreeNode root7 = new TreeNode(7);
        TreeNode root8 = new TreeNode(8);
//        TreeNode root9 = new TreeNode(9);
        root1.left = root2;
        root1.right = root3;
        root2.left = root4;
        root2.right = root5;
        root3.left = root6;
        root3.right = root7;
        root4.right = root8;
//        root8.left = root9;
        return root1;
    }

    public TreeNode createBinaryTree2() {
        TreeNode root2 = new TreeNode(2);
        TreeNode root4 = new TreeNode(4);
        TreeNode root5 = new TreeNode(5);
        TreeNode root8 = new TreeNode(8);
       root2.left = root4;
       root2.right = root5;
       root4.right = root8;
        return root2;
    }

    //判断两棵树是否是相同的树
    public boolean isSameTree(TreeNode p, TreeNode q) {
        //先判断两棵树是否都为空
        if(p == null && q == null){
            return true;
        }
        if(p == null || q == null){
            return false;
        }
        if(p.val != q.val){
            return false;
        }else {
            boolean left = isSameTree(p.left, q.left);
            boolean right = isSameTree(p.right, q.right);
            return left && right;
        }
    }

    //求一棵树是不是另一棵树的子树
    public boolean isSubTree(TreeNode p, TreeNode q) {
        if(p == null && q == null){
            return true;
        }
        if(p == null || q == null){
            return false;
        }
        if(isSameTree(p, q)){
            return true;
        }
        boolean left = isSubTree(p.left, q);
        boolean right = isSubTree(p.right, q);
        return left || right;
    }

    //翻转二叉树
    public void invertTree(TreeNode root) {
        if(root == null){
            return;
        }
        //首先将根节点翻转
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        invertTree(root.left);
        invertTree(root.right);
    }

    //判断一颗二叉树是否是平衡二叉树
    public boolean isBalancedTree(TreeNode root) {
        if(root == null){
            return true;
        }
        //求左树高度
        int leftHeight = treeHeight(root.left);
        int rightHeight = treeHeight(root.right);
        if(Math.abs(leftHeight - rightHeight) > 1){
            return false;
        }
        boolean left = isBalancedTree(root.left);
        boolean right = isBalancedTree(root.right);
        return left && right;
    }
    //求二叉树的深度
    private int treeHeight(TreeNode root) {
        if(root == null){
            return 0;
        }
        int left = treeHeight(root.left);
        int right = treeHeight(root.right);
        return Math.max(left, right) + 1;
    }

    //判断一棵树是否是对称二叉树
    public boolean isSymmetric(TreeNode root) {
        if(root == null){
            return true;
        }
        return isSymmetricChild(root.left, root.right);
    }
    private boolean isSymmetricChild(TreeNode p, TreeNode q) {
        if(p == null && q == null){
            return true;
        }
        if(p == null || q == null){
            return false;
        }
        //先判断根是否相同
        if(p.val != q.val){
            return false;
        }
        boolean left = isSymmetricChild(p.left,q.right);
        boolean right = isSymmetricChild(p.right,q.left);
        return left && right;
    }


    //根据数组(顺序为前序遍历)创建一个二叉树
    private int i;
    public TreeNode createTreeByArray(String str) {
        TreeNode root = null;
        if(i < str.length()){
            if(str.charAt(i) != '#'){
                root = new TreeNode(str.charAt(i));
                i++;
                root.left = createTreeByArray(str);
                root.right = createTreeByArray(str);
            }else {
                i++;
            }
        }
        return root;
    }


    //求两个节点的最近公共祖先
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null){
            return null;
        }
        //先看自己是否是最近公共祖先
        if(root == p || root == q){
            return root;
        }
        //再看左右返回的节点是否为空
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if(left == null && right == null){
            return null;
        }
        if(left == null){
            return right;
        }
        if(right == null){
            return left;
        }
        return root;
    }
}
